layui.use(["element", 'layer', 'form'], function () {
  var element = layui.element,
    form = layui.form,
    layer = layui.layer;
});
// AssetCode  资产代码  

var vm = new Vue({
  el: '#app',
  data: {
    status: localStorage.getItem('status'),
    ArrData: [
      {
        AssetClass: '1',
        AssetName: '1',
        amount: '100,000',
        proportion: '10%',
        price: '5.5%',
        CompanyName: '企业A',
        mark: '',
        clasN:'非标准化债权类资产',AssetN:'非标资产A2'
      },
      {
        AssetClass: '2',
        AssetName: '2',
        amount: '200,000',
        proportion: '20%',
        price: '5%',
        CompanyName: '企业A',
        mark: '' ,clasN:'债券',AssetN:'债券资产A2'
      },
      {
        AssetClass: '3',
        AssetName: '3',
        amount: '400,000',
        proportion: '40%',
        price: '4.5%',
        CompanyName: '同业C',
        mark: '' ,clasN:'同业存款',AssetN:'同业存款资产C1'
      },
      {
        AssetClass: '4',
        AssetName: '4',
        amount: '100,000',
        proportion: '10%',
        price: '6%',
        CompanyName: '同业D',
        mark: ''    ,clasN:'同业存单',AssetN:'同业存单资产D1'
      },
      {
        AssetClass: '5',
        AssetName: '5',
        amount: '50,000',
        proportion: '5%',
        price: '4%',
        CompanyName: '同业D',
        mark: '',clasN:'回购',AssetN:'回购资产D1'
      }
    ],
    totalVisible: false,
    total: 1000000,
    syl_txt:'产品组合收益率：4.475%',
    ypzje_txt:'理财产品已配置资产金额：850,000万元',
    spzje_txt:'理财产品剩余可配置金额：150,000万元',
    spzje:0
  },
  mounted: function () {
    this.init()
  },
  methods: {
    init: function () {
      localStorage.setItem('navigation','<a href="#">首页</a><span lay-separator="">/</span><a href="">大类资产配置模型</a><span lay-separator="">/</span><a><cite>产品资产配置</cite></a>');
      this.setPrice();
    },
    del: function(index) {
      this.ArrData.splice(index, 1);
      this.setPrice();
    },
    setPortion: function (index) {
      var obj = this.ArrData[index];
      var am = Number(obj.amount.replace(/,/g, ''));
      if(obj.clasN=='非标准化债权类资产' && am>120000){
        layer.alert('投资单一债务人的非标资产余额不得超过<br>理财产品总额的10%。', {
          icon: 2,
          btnAlign: 'c',
          shade: false,
          end:function(){
            vm.ArrData[index].amount ='100,000';
          }
        });
        return;
      }
      if(obj.clasN=='债券' && am>200000){
        layer.alert('每只理财产品持有单只债券的市值不得超过<br>该理财产品净资产的20%。', {
          icon: 2,
          btnAlign: 'c',
          shade: false,
          end:function(){
            vm.ArrData[index].amount ='200,000';
          }
        });
        return;
      }
      if(obj.clasN=='同业存款' && am>400000){
        layer.alert('<b>同业存款资产C1</b>最大可投资金额<br>为400,000万元。', {
          icon: 2,
          btnAlign: 'c',
          shade: false,
          end:function(){
            vm.ArrData[index].amount ='400,000';
          }
        });
        return;
      }
      if(obj.clasN=='同业存单' && am>100000){
        layer.alert('每只理财产品持有单只存单的市值不得超过<br>该理财产品净资产的10%。', {
          icon: 2,
          btnAlign: 'c',
          shade: false,
          end:function(){
            vm.ArrData[index].amount ='100,000';
          }
        });
        return;
      }
      if(obj.clasN=='回购' && am>50000){
        layer.alert('<b>回购资产D1</b>最大可投资金额为50,000万元。', {
          icon: 2,
          btnAlign: 'c',
          shade: false,
          end:function(){
            vm.ArrData[index].amount ='50,000';
          }
        });
        return;
      }
      this.ArrData[index].proportion = (am / this.total) * 100 + '%';
      this.setPrice();
    },
    setPrice: function () {
      var syl = 0;
      var yp = 0;
      this.ArrData.forEach(function (item, index) {
        syl += Number(item.proportion.replace("%", '')) * Number(item.price.replace("%", '')) * 0.0001;
        yp += Number(item.amount.replace(/,/g, ''));
      });
      this.syl_txt = "组合收益率：" + (syl * 100).toFixed(2) + "%";
      this.ypzje_txt = '理财产品已配置资产金额：'+yp.toLocaleString()+'万元';
      this.spzje = this.total-yp;
      this.spzje_txt = '理财产品剩余可配置金额：'+(this.total-yp).toLocaleString()+'万元';
    },
    addItem: function () {
      vm.ArrData.push({
        AssetClass: '0',
        amount: '',
        proportion: '',
        price: '',
        mark: '',
        clasN:'请选择',
        AssetName:0,
        AssetN:'请选择'
      });
    },
    submitForm: function () {
      var pri = this.spzje;
      if(pri>=0){
        layer.confirm('操作成功！', {
          icon: 1,
          title: '提示',
          shade: false,          
          btnAlign: 'c',
          btn: ['完成', '查看详情'],
          btnAlign:"c"
        }, function () {
          localStorage.setItem("status",1); // 设置已经完成配置
          parent.location.href = '../../main.html?url=html/DLZCPZMX/CPZCCCCX.html&id=25';
        }, function () {
          window.location.href = 'CCXQ_3.html';
        });
      }
      if(pri<0){
        layer.alert('资产配置金额超过待投资总金额，请修改后再次保存！', {
          icon: 2,
          btnAlign: 'c',
          shade: false
        })
      }
      // if(pri>0){
      //   layer.alert('尚有待投资金额未配置完，请继续配置！', {
      //     icon: 0,
      //     btnAlign: 'c',
      //     shade: false
      //   })
      // }
    },
    setData:function(index,data,num){
      if(num==2){
        this.ArrData[index].AssetN = data;
      }
      else{
        this.ArrData[index].clasN = data;
      }
    }
  }
})